package com.lkd.controller;

import com.lkd.dto.SkuClassDto;
import com.lkd.entity.SkuClass;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.SkuClassService;
import com.lkd.vo.SkuClassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/skuClass")
public class SkuClassController {

    @Autowired
    private SkuClassService skuClassService;

    @PostMapping
    public Result saveSkuClass(@RequestBody SkuClassDto skuClassDto){
        skuClassService.save(skuClassDto);
        return Result.success();
    }

    @GetMapping("/search")
    public Result findByCondition(
            @RequestParam(name = "className",required = false)String className,
            @RequestParam(name = "pageIndex",defaultValue = "1")Integer pageIndex,
            @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize
            ){
        PageBean<SkuClassVo> skuClassPageBean = skuClassService.findByCondition(className,pageIndex,pageSize);
        return Result.success(skuClassPageBean);
    }

    @GetMapping("/{classId}")
    public Result findByClassId(@PathVariable(name = "classId")Integer classId){
        SkuClassVo skuClassVo = skuClassService.findByClassId(classId);
        return Result.success(skuClassVo);
    }

    @PutMapping
    public Result update(@RequestBody SkuClassDto skuClassDto){
        skuClassService.update(skuClassDto);
        return Result.success(true);
    }

    @DeleteMapping("/{classId}")
    public Result delete(@PathVariable(name = "classId")Integer classId){
        skuClassService.delete(classId);
        return Result.success(true);
    }
}
